package name.jautzy.esi.client;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.event.Listener;


public class ImportDbListener implements Listener<ButtonEvent> {
	private ContentPanel parent;
	
	public ImportDbListener (ContentPanel parent) {
		this.parent = parent;
	}
	
	public void handleEvent(ButtonEvent ce) {  
        if (!ce.<ToggleButton> getComponent().isPressed()) {  
          return;  
        }  

        final FormPanel form = new FormPanel();  
        form.setHeading("Upload CSV converted Eve Database (development only)");  
        form.setFrame(true);  
        form.setAction("/esi/evedbupload");  
        form.setEncoding(Encoding.MULTIPART);  
        form.setMethod(Method.POST);  
        form.setButtonAlign(HorizontalAlignment.LEFT);  
        form.setWidth(350);   

        FileUploadField file = new FileUploadField();  
        file.setAllowBlank(true);  
        file.setFieldLabel("Equipments");  
        file.setName("equipfile");
        form.add(file); 
        
        FileUploadField file2 = new FileUploadField();  
        file.setAllowBlank(true);  
        file2.setFieldLabel("Attributes");  
        file2.setName("attfile");
        form.add(file2); 
        
        FileUploadField file3 = new FileUploadField();  
        file.setAllowBlank(true);   
        file3.setFieldLabel("Effects");  
        file3.setName("efffile");
        form.add(file3); 
        
        FileUploadField file4 = new FileUploadField();  
        file.setAllowBlank(true);    
        file4.setFieldLabel("Categories");  
        file4.setName("catfile");
        form.add(file4); 
        
        FileUploadField file5 = new FileUploadField();  
        file.setAllowBlank(true);   
        file5.setFieldLabel("Groups");  
        file5.setName("groupfile");
        form.add(file5); 

        Button btn = new Button("Submit");  
        btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
      
        	@Override  
        	public void componentSelected(ButtonEvent ce) {  
        		if (!form.isValid()) {  
        			return;  
        		}  
          
        		form.submit();  
          
        		MessageBox.info("Action", "You file was uploaded", null);  
        	}  
        });  
        form.addButton(btn);  
        parent.removeAll();  
	    parent.add(form);  
	    parent.layout();   
	}
}
